package com.yanqu.road.server.cmd.activity.seacraftsouthsea;

import com.yanqu.road.entity.activity.*;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.pb.activity.SeacraftSouthSeaProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.GeneralActivityRankMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftSouthSeaMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

/**
 * @Description
 * @Author cwq
 * @Data 2022/1/20 15:28
 */
@Cmd(code = GameProtocol.S_SEACRAFT_SOUTH_SEA_RECEIVE_UNION_RANK, desc = "海上争霸 南海丝路 领取联盟排行榜奖励")
public class SeacraftSouthSeaReceiveUnionRankCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        SeacraftSouthSeaProto.SeacraftSouthSeaReceiveUnionRankReqMsg reqMsg = SeacraftSouthSeaProto.SeacraftSouthSeaReceiveUnionRankReqMsg.parseFrom(packet.getBytes());
//        ret = SeacraftSouthSeaMgr.canEntryActivity(player,reqMsg.getActivityId());
//        if(ret != 0){
//            return;
//        }

        BaseActivityData baseActivityData = player.getModule(ActivityModule.class).getBaseActivityData(reqMsg.getActivityId());
        if (null == baseActivityData) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(reqMsg.getActivityId());
        if(null == activityInfo){
            activityInfo = baseActivityData.getActivityInfo();
        }
        if (!ActivityMgr.activityInRewardPeriod(activityInfo)) {//不在领奖期
            ret = GameErrorCode.E_ACTIVITY_NO_IN_GET_REWARD_TIME;
            return ;
        }
        if (!ActivityMgr.activityInCanRewardPeriod(activityInfo)) {
            ret = GameErrorCode.E_ACTIVITY_IN_REWARD_SETTLEMENT_TIME;
            return ;
        }

        if (activityInfo.getType() == eActivityType.SeacraftActivity.getValue()) {
            UserActivityConditionData userActivityConditionData = baseActivityData.getFirstUserActivityConditionDataByType(eGamePlayerEventType.SeacraftSouthSeaUnionRank.getValue());
            if (null == userActivityConditionData) {
                ret = GameErrorCode.E_ACTIVITY_NO_JOIN;
                return ;
            }
            if (userActivityConditionData.isGetReward()) {
                ret = GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
                return ;
            }
        } else if (activityInfo.getType() == eActivityType.PeakSeacraftActivity.getValue()) {
            UserActivityConditionData userActivityConditionData = baseActivityData.getFirstUserActivityConditionDataByType(eGamePlayerEventType.PeakSeacraftUnionRank.getValue());
            if (null == userActivityConditionData) {
                ret = GameErrorCode.E_ACTIVITY_NO_JOIN;
                return ;
            }
            if (userActivityConditionData.isGetReward()) {
                ret = GameErrorCode.E_ACTIVITY_REWARD_IS_GET;
                return ;
            }
        }


        player.sendUnionActivityGroupPacket(CrossProtocol.C2_SEACRAFT_SOUTH_SEA_RECEIVE_UNION_RANK, reqMsg.toBuilder(), reqMsg.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            SeacraftSouthSeaProto.SeacraftSouthSeaReceiveUnionRankRespMsg.Builder resp = SeacraftSouthSeaProto.SeacraftSouthSeaReceiveUnionRankRespMsg.newBuilder();
            resp.setRet(ret);
            player.sendPacket(ClientProtocol.U_SEACRAFT_SOUTH_SEA_GET_UNION_RANK,resp);
        }
    }
}
